package org.totoro.structure.time;

import lombok.Getter;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 时间轮
 *
 * @author daocr
 * @date 2020/4/7
 */
public class TimingWheel {


    /**
     * 桶
     */
    public static class Bucket {
        @Getter
        private TimeTask task;
    }

    /**
     * 定时任务
     */
    public static class TimeTask {

        /**
         * 定时时间
         */
        @Getter
        private Long time;

        /**
         * 状态
         */
        @Getter
        private State state;
        /**
         * 任务
         */
        private Runnable runnable;
    }

    /**
     * 循环周期
     */
    public static class Cycle {

        /**
         * 父周期
         */
        private Cycle parentCycle;

        /**
         * 周期类型
         */
        private CycleType cycleType;


        /**
         * 未来时间
         */
        private ConcurrentHashMap<Integer, CopyOnWriteArrayList<TimeTask>> futureTime;

        /**
         * 桶
         */
        private Bucket[] buckets;

        public Cycle(CycleType cycleType) {
            this.cycleType = cycleType;
            buckets = new Bucket[cycleType.getCycle()];
        }
    }


}
